package com.cqut.ktpspring.service.impl;

import com.cqut.ktpspring.mapper.AnswerMapper;
import com.cqut.ktpspring.mapper.WorkMapper;
import com.cqut.ktpspring.pojo.Work;
import com.cqut.ktpspring.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AnswerServiceimpl implements AnswerService {
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private WorkMapper workMapper;
    @Override
    @Transactional
    public void revise(String userId, String wId, Integer score) {
        Work work = workMapper.selectById(wId);
        answerMapper.revise(userId,wId,score);
        if (work.getMaxScore()==0){
            work.setMaxScore(score);
            workMapper.updateById(work);
        }
        else {
            if(work.getMaxScore()<score){
                work.setMaxScore(score);
                workMapper.updateById(work);
            }
        }
        answerMapper.changeJudge(wId);
    }

    @Override
    @Transactional
    public void submit(String userId, String wId, String answerText) {
        answerMapper.submit(userId,wId,answerText);
        answerMapper.changeSubmit(wId);
    }

    @Override
    public void addAnswer(String userId, String wId) {
        answerMapper.addAnswer(userId, wId);
    }
}
